package com.example.xd.gupmodule;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.filter.GPUImage3x3ConvolutionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImage3x3TextureSamplingFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageAddBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageAlphaBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageBilateralBlurFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageBoxBlurFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageBrightnessFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageBulgeDistortionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageCGAColorspaceFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageChromaKeyBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorBalanceFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorBurnBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorDodgeBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorInvertFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorMatrixFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageContrastFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageCrosshatchFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDarkenBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDifferenceBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDilationFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDirectionalSobelEdgeDetectionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDissolveBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageDivideBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageEmbossFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageExclusionBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageExposureFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFalseColorFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilterGroup;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGammaFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGaussianBlurFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGlassSphereFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageGrayscaleFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHalftoneFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHardLightBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHazeFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHighlightShadowFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHueBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageHueFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageKuwaharaFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLaplacianFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLevelsFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLightenBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLinearBurnBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLookupFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLuminanceFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLuminanceThresholdFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageLuminosityBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageMixBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageMonochromeFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageMultiplyBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageNonMaximumSuppressionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageNormalBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImagePixelationFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImagePosterizeFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageRGBDilationFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageRGBFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSaturationBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSaturationFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageScreenBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSepiaToneFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSharpenFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSketchFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSmoothToonFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSobelEdgeDetectionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSobelThresholdFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSoftLightBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSolarizeFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSourceOverBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSphereRefractionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSubtractBlendFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSwirlFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageThresholdEdgeDetectionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageToneCurveFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageToonFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageTransformFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageTwoInputFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageTwoPassFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageTwoPassTextureSamplingFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageVibranceFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageVignetteFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageWeakPixelInclusionFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageWhiteBalanceFilter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageZoomBlurFilter;

/**
 * @auther XuDan on 2020/5/8.
 */
public class GPUImageUtil {
    private static GPUImageFilter filter;

    /**
     * 获取过滤器
     *
     * @param GPUFlag
     * @return 滤镜类型
     */
    public static GPUImageFilter getFilter(int GPUFlag) {
        switch (GPUFlag) {
            case 1:
                filter = new GPUImage3x3ConvolutionFilter();
                break;
            case 2:
                filter = new GPUImageAddBlendFilter();
                break;
            case 3:
                filter = new GPUImageAlphaBlendFilter();
                break;
            case 4:
                filter = new GPUImage3x3TextureSamplingFilter();
                break;
            case 5:
                filter = new GPUImageBoxBlurFilter();
                break;
            case 6:
                filter = new GPUImageBrightnessFilter();//亮度
                break;
            case 7:
                filter = new GPUImageBulgeDistortionFilter();
                break;
            case 8:
                filter = new GPUImageCGAColorspaceFilter();
                break;
            case 9:
                filter = new GPUImageChromaKeyBlendFilter();
                break;
            case 10:
                filter = new GPUImageBilateralBlurFilter();
                break;
            case 11:
                filter = new GPUImageExposureFilter();//曝光
                break;
            case 12:
                filter = new GPUImageContrastFilter();//对比度
                break;
            case 13:
                filter = new GPUImageColorBalanceFilter();
                break;
            case 14:
                filter = new GPUImageSaturationFilter(4);//饱和度
                break;
            case 15:
                filter = new GPUImageGammaFilter();//伽马线
                break;
            case 16:
                filter = new GPUImageColorInvertFilter();//反色
                break;
            case 17:
                filter = new GPUImageLevelsFilter();//色阶
                break;
            case 18:
                filter = new GPUImageGrayscaleFilter();//灰阶
                break;
            case 19:
                filter = new GPUImageThresholdEdgeDetectionFilter();
                break;
            case 20:
                filter = new GPUImageToneCurveFilter();
                break;
            case 21:
                filter = new GPUImageToonFilter();
                break;
            case 22:
                filter = new GPUImageTransformFilter();
                break;
            case 23:
                filter = new GPUImageTwoInputFilter("");
                break;
            case 24:
                filter = new GPUImageTwoPassFilter("", "", "", "");
                break;
            case 25:
                filter = new GPUImageTwoPassTextureSamplingFilter("", "", "", "");
                break;
            case 26:
                filter = new GPUImageVibranceFilter();
                break;
            case 27:
                filter = new GPUImageVignetteFilter();
                break;
            case 28:
                filter = new GPUImageWeakPixelInclusionFilter();
                break;
            case 29:
                filter = new GPUImageWhiteBalanceFilter();
                break;
            case 30:
                filter = new GPUImageZoomBlurFilter();
                break;
            case 31:
                filter = new GPUImageSaturationBlendFilter();
                break;
            case 32:
                filter = new GPUImageScreenBlendFilter();
                break;
            case 33:
                filter = new GPUImageSepiaToneFilter();
                break;
            case 34:
                filter = new GPUImageSharpenFilter();
                break;
            case 35:
                filter = new GPUImageSketchFilter();
                break;
            case 36:
                filter = new GPUImageSmoothToonFilter();
                break;
            case 37:
                filter = new GPUImageSobelEdgeDetectionFilter();
                break;
            case 38:
                filter = new GPUImageSobelThresholdFilter();
                break;
            case 39:
                filter = new GPUImageSoftLightBlendFilter();
                break;
            case 40:
                filter = new GPUImageSolarizeFilter();
                break;
            case 41:
                filter = new GPUImageSourceOverBlendFilter();
                break;
            case 42:
                filter = new GPUImageSphereRefractionFilter();
                break;
            case 43:
                filter = new GPUImageSubtractBlendFilter();
                break;
            case 44:
                filter = new GPUImageSwirlFilter();
                break;
            case 45:
                filter = new GPUImageColorBlendFilter();
                break;
            case 46:
                filter = new GPUImageColorBurnBlendFilter();
                break;
            case 47:
                filter = new GPUImageColorDodgeBlendFilter();
                break;
            case 48:
                filter = new GPUImageColorMatrixFilter();
                break;
            case 49:
                filter = new GPUImageCrosshatchFilter();
                break;
            case 50:
                filter = new GPUImageDifferenceBlendFilter();
                break;
            case 51:
                filter = new GPUImageDarkenBlendFilter();
                break;
            case 52:
                filter = new GPUImageDilationFilter();
                break;
            case 53:
                filter = new GPUImageDirectionalSobelEdgeDetectionFilter();
                break;
            case 54:
                filter = new GPUImageDissolveBlendFilter();
                break;
            case 55:
                filter = new GPUImageDivideBlendFilter();
                break;
            case 56:
                filter = new GPUImageEmbossFilter();
                break;
            case 57:
                filter = new GPUImageExclusionBlendFilter();
                break;
            case 58:
                filter = new GPUImageFilter();
                break;
            case 59:
                filter = new GPUImageFalseColorFilter();
                break;
            case 60:
                filter = new GPUImageFilterGroup();
                break;
            case 61:
                filter = new GPUImageGaussianBlurFilter();
                break;
            case 62:
                filter = new GPUImageGlassSphereFilter();
                break;
            case 63:
                filter = new GPUImageGrayscaleFilter();
                break;
            case 64:
                filter = new GPUImageHalftoneFilter();
                break;
            case 65:
                filter = new GPUImageHardLightBlendFilter();
                break;
            case 66:
                filter = new GPUImageHazeFilter();
                break;
            case 67:
                filter = new GPUImageHighlightShadowFilter();
                break;
            case 68:
                filter = new GPUImageHueBlendFilter();
                break;
            case 69:
                filter = new GPUImageHueFilter();
                break;
            case 70:
                filter = new GPUImageKuwaharaFilter();
                break;
            case 71:
                filter = new GPUImageLaplacianFilter();
                break;
            case 72:
                filter = new GPUImageLightenBlendFilter();
                break;
            case 73:
                filter = new GPUImageLinearBurnBlendFilter();
                break;
            case 74:
                filter = new GPUImageLookupFilter();
                break;
            case 75:
                filter = new GPUImageLuminanceFilter();
                break;
            case 76:
                filter = new GPUImageLuminanceThresholdFilter();
                break;
            case 77:
                filter = new GPUImageLuminosityBlendFilter();
                break;
            case 78:
                filter = new GPUImageMixBlendFilter("");
                break;
            case 79:
                filter = new GPUImageMonochromeFilter();
                break;
            case 80:
                filter = new GPUImageMultiplyBlendFilter();
                break;
            case 81:
                filter = new GPUImageNonMaximumSuppressionFilter();
                break;
            case 82:
                filter = new GPUImageNormalBlendFilter();
                break;
            case 83:
                filter = new GPUImagePixelationFilter();
                break;
            case 84:
                filter = new GPUImagePosterizeFilter();
                break;
            case 85:
                filter = new GPUImageRGBDilationFilter();
                break;
            case 86:
                filter = new GPUImageRGBFilter();
                break;

        }
        return filter;
    }

    public static Bitmap getGpuImage(Context context, GPUImage gpuImage, int FilterFlag) {
        AssetManager as = context.getAssets();
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            is = as.open("link.jpg");
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            Log.e("GPUImage", "Error");
        }

        // 使用GPUImage处理图像
        gpuImage = new GPUImage(context);
        gpuImage.setImage(bitmap);
        gpuImage.setFilter(getFilter(FilterFlag));
        bitmap = gpuImage.getBitmapWithFilterApplied();
        return bitmap;
    }

    /*
     * 从网络获取图片
     * @param url
     * @return
     */
    public static Bitmap getGPUImageFromURL(String url) {
        Bitmap bitmap = null;
        try {
            URL imageUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
            // 获得图像的字符流
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bitmap = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
